package com.main.mq;

import com.main.common.CustomException;
import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class MessageRocketmq {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    /**
     * 发送消息
     *
     * @param mail 邮件
     * @param code 代码
     */
    //    @Async
    public void sendMessage(String mail, String code) {
        Map map = new HashMap();
        map.put("mail", mail);
        map.put("code", code);
//        rocketMQTemplate.convertAndSend("order_id",id);//同步
        SendCallback callBack = new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
//                System.out.println(Thread.currentThread().getId() + "--3");
                System.out.println("消息发送成功");
                String msg = sendResult.getSendStatus().toString();
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息发送失败！！！！！");
                String msg = throwable.getMessage().toString();
            }
        };
//        System.out.println(Thread.currentThread().getId() + "--2");
        rocketMQTemplate.asyncSend("user_code", map, callBack);//发送异步消息
    }
}

